// 包含应用钟所有请求函数的模块
import http from "./httpRequest";

// const BASE = "http://localhost:3001";
// 前端服务启动在3000端口  直接请求3001会产生跨域   所以要用代理服务器转发请求  代理在package.json中设置

// 因为已经设置代理服务器了  所以这里url不需要写的像上面一样了 发送的请求已经可以直接通过代理了
const BASE = "";

// 登录接口请求 需要用户名和密码
export function reqLogin(username: string, password: string): any {
  // 需要传的信息都保存在打他中, 也就是要像下面一样{ username, password }
  return http(BASE + "/login", { username, password }, "POST");
}

//添加用户
export function addUser(user: any) {
  return http(BASE + "/manage/user/add", user, "POST");
}

// 请求天气api的函数
export function reqWeather(): any {
  // 101110101是西安的城市代号
  const url = "https://api.help.bj.cn/apis/weather/?id=101110101";
  return http(url, {}, "GET");
}

// 要多查看后端api文档
// 获取一级/二级类型列表
export function reqCategory(parentId: string): any {
  return http(BASE + "/manage/category/list", { parentId });
}

// 添加分类
export function addCategory(parentId: string, categoryName: string): any {
  return http(
    BASE + "/manage/category/add",
    { parentId, categoryName },
    "POST"
  );
}

// 更新分类
export function updateCategory(parentId: string, categoryName: string): any {
  return http(
    BASE + "/manage/category/update",
    { parentId, categoryName },
    "POST"
  );
}

// 获取商品分页列表 参数是页码和每一页要显示多少项 默认使用GET请求方法
export function reqProduct(pageNum: number, pageSize: number): any {
  return http(BASE + "/manage/product/list", { pageNum, pageSize });
}

// 搜索商品分页列表(根据商品名称或者商品描述),searchType确定搜索类型-名称还是描述,(productName/productDesc)
export function reqSearchProduct(
  // 参数searchType 是按照Name还是Desc来搜索
  // 参数pageNum是分页的页数
  // 参数pageSize是每页的展示数量
  // 参数searchName是搜索的关键字
  searchType: string,
  pageNum: number,
  pageSize: number,
  searchName: string
): any {
  return http(BASE + "/manage/product/search", {
    pageNum,
    pageSize,
    [searchType]: searchName,
  });
}

// 获取商品所处分类的请求
export function reqCategoryName(categoryId: string): any {
  return http(BASE + "/manage/category/info", {
    categoryId,
  });
}

// 删除后台中上传的图片
export function reqDeleteImg(name: any): any {
  return http(
    BASE + "/manage/img/delete",
    {
      name,
    },
    "POST"
  );
}

// 添加新的商品以及商品信息  和修改已有商品信息的共有接口
export function addOrUpdateProduct(productInfo: any): any {
  // 根据传入的参数是否有_id来判断是添加还是更新修改信息
  let temp = "";
  if (productInfo._id) {
    temp = "update";
  } else {
    temp = "add";
  }
  let url = BASE + "/manage/product/" + temp;
  return http(
    url,
    {
      productInfo,
    },
    "POST"
  );
}

// 获取role的接口请求
export function reqRoles(): any {
  return http(BASE + "/manage/role/list");
}

// 添加角色接口
export function reqAddRole(name: any): any {
  return http(
    BASE + "/manage/role/add",
    {
      name,
    },
    "POST"
  );
}

// 更新角色权限接口
export function reqUpdateRole(role: any): any {
  return http(BASE + "/manage/role/update", role, "POST");
}

// 获取所有用户信息列表
export function reqUsers(): any {
  return http(BASE + "/manage/user/list");
}

// 删除用户请求
export function reqDeleteUsers(_id: any): any {
  return http(BASE + "/manage/user/delete", { _id }, "POST");
}

// 创建用户请求
export function reqAddUsers(userInfo: any): any {
  return http(BASE + "/manage/user/add", { userInfo }, "POST");
}

// 更新用户信息请求
export function reqUpdateUsers(userInfo: any): any {
  return http(BASE + "/manage/user/update", { userInfo }, "POST");
}
